<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller\Home;
use App\Http\Requests\ZhongzhiRequest;
use Hash;
class ChongzhiController extends Controller
{
    //订单列表
    public function insert(ZhongzhiRequest $request)
    {   
        
        // echo '充值';  
        if(!session('name')){
          return view('home.login.login');
        }
        $pdw = $request->input('password');
        $ress = DB::table('user')->where('username',session('name'))->first();
        $password = $ress->password;
        // dd($pdw);
        if(!Hash::check($pdw,$password)){
          return view('home.login.login');
            // echo '1111';
        }
        $uname = session('name');
        $lice_name = '淘气一哥店主';
        $account = $request->input('account');
        // dd($account);
        $users = DB::table('user')->where('username',$uname)->first();
        $uid = $users->id;
        $lice_users = DB::table('dayingjia')->where('lice_name',$lice_name)->first();
        $lid = $lice_users->id;
        // dd($lid);
        //开启事务
        DB::beginTransaction();
        $ures = DB::update('update user set account = account + '.$account.' where id = '.$uid);
        if(!$ures == 1){
          DB::rollBack();//事务回滚
          dd('充值失败');
        }
        // dd($ures);
        $dres = DB::update('update dayingjia set dayingjia = dayingjia - '.$account.' where id = '.$lid);
        // dd($res);
        if(!$dres == 1){
            DB::rollBack();
            dd('扣钱失败');
        }
        DB::commit();//事物提交
        // return back('充值成功');
        return view('welcome');


    } 
    
}
